home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gp_dosfe.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  4.2 KB  |  152 lines

  1. /* Copyright (C) 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gp_dosfe.c,v 1.2 2000/09/19 19:00:24 lpd Exp $ */
  20. /* MS-DOS file enumeration. */
  21. #include "stdio_.h"
  22. #include <fcntl.h>
  23. #include "dos_.h"
  24. #include "memory_.h"
  25. #include "string_.h"
  26. #include "gstypes.h"
  27. #include "gsmemory.h"
  28. #include "gsstruct.h"
  29. #include "gp.h"
  30. #include "gsutil.h"
  31.  
  32. struct file_enum_s {
  33.     ff_struct_t ffblk;
  34.     char *pattern;        /* orig pattern + modified pattern */
  35.     int patlen;            /* orig pattern length */
  36.     int pat_size;        /* allocate space for pattern */
  37.     int head_size;        /* pattern length through last */
  38.     /* :, / or \ */
  39.     int first_time;
  40.     gs_memory_t *memory;
  41. };
  42. gs_private_st_ptrs1(st_file_enum, struct file_enum_s, "file_enum",
  43.             file_enum_enum_ptrs, file_enum_reloc_ptrs, pattern);
  44.  
  45. /* Initialize an enumeration.  Note that * and ? in a directory */
  46. /* don't work, and \ is taken literally unless a second \ follows. */
  47. file_enum *
  48. gp_enumerate_files_init(const char *pat, uint patlen, gs_memory_t * mem)
  49. {
  50.     file_enum *pfen = gs_alloc_struct(mem, file_enum, &st_file_enum, "gp_enumerate_files");
  51.     int pat_size = 2 * patlen + 1;
  52.     char *pattern;
  53.     char *p;
  54.     int hsize = 0;
  55.     int i;
  56.     int dot = 0;
  57.  
  58.     if (pfen == 0)
  59.     return 0;
  60.  
  61.     /* pattern could be allocated as a string, */
  62.     /* but it's simpler for GC and freeing to allocate it as bytes. */
  63.  
  64.     pattern = (char *)gs_alloc_bytes(mem, pat_size,
  65.                      "gp_enumerate_files(pattern)");
  66.     if (pattern == 0)
  67.     return 0;
  68.     memcpy(pattern, pat, patlen);
  69.     p = pattern + patlen;
  70.     for (i = 0; i < patlen; i++) {
  71.     switch (pat[i]) {
  72.         case '*':
  73.         /* Skip to . or end of string so DOS can do it. */
  74.         *p++ = '*';
  75.         while (i < patlen && pat[i] != '.')
  76.             i++;
  77.         if (i == patlen && !dot) {    /* DOS doesn't interpret * alone as */
  78.             /* matching all files; we need *.*. */
  79.             *p++ = '.';
  80.             *p++ = '*';
  81.         }
  82.         i--;
  83.         continue;
  84.         case '.':
  85.         dot = 1;
  86.         break;
  87.         case '\\':
  88.         if (i + 1 < patlen && pat[i + 1] == '\\')
  89.             i++;
  90.         /* falls through */
  91.         case ':':
  92.         case '/':
  93.         hsize = p + 1 - (pattern + patlen);
  94.         dot = 0;
  95.     }
  96.     *p++ = pat[i];
  97.     }
  98.     *p = 0;
  99.     pfen->pattern = pattern;
  100.     pfen->patlen = patlen;
  101.     pfen->pat_size = pat_size;
  102.     pfen->head_size = hsize;
  103.     pfen->memory = mem;
  104.     pfen->first_time = 1;
  105.     return pfen;
  106. }
  107.  
  108. /* Enumerate the next file. */
  109. private const string_match_params smp_file =
  110. {'*', '?', -1, true};
  111.  
  112. uint
  113. gp_enumerate_files_next(file_enum * pfen, char *ptr, uint maxlen)
  114. {
  115.     int code;
  116.     char *p, *q;
  117.     uint len;
  118.     const char *fpat = pfen->pattern + pfen->patlen;
  119.  
  120.   top:if (pfen->first_time) {
  121.     code = dos_findfirst(fpat, &pfen->ffblk);
  122.     pfen->first_time = 0;
  123.     } else
  124.     code = dos_findnext(&pfen->ffblk);
  125.     if (code != 0) {        /* All done, clean up. */
  126.     gp_enumerate_files_close(pfen);
  127.     return ~(uint) 0;
  128.     }
  129.     if (maxlen < 13 + pfen->head_size)
  130.     return maxlen + 1;    /* cop out! */
  131.     memcpy(ptr, fpat, pfen->head_size);
  132.     for (p = &pfen->ffblk.ff_name[0], q = ptr + pfen->head_size; *p; p++)
  133.     if (*p != ' ')
  134.         *q++ = *p;
  135.     len = q - ptr;
  136.     /* Make sure this file really matches the pattern. */
  137.     if (!string_match(ptr, len, pfen->pattern, pfen->patlen, &smp_file))
  138.     goto top;
  139.     return len;
  140. }
  141.  
  142. /* Clean up the file enumeration. */
  143. void
  144. gp_enumerate_files_close(file_enum * pfen)
  145. {
  146.     gs_memory_t *mem = pfen->memory;
  147.  
  148.     gs_free_object(mem, pfen->pattern,
  149.            "gp_enumerate_files_close(pattern)");
  150.     gs_free_object(mem, pfen, "gp_enumerate_files_close");
  151. }
  152.